const formatTime = (date, dfm) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return dfm.replace('yyyy', year).replace('MM', formatNumber(month)).replace('dd', formatNumber(day)).replace('hh', formatNumber(hour)).replace('mm', formatNumber(minute)).replace('ss', formatNumber(second))
  //return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const isBlank = str => {
  if (str == undefined || str == "") {
    return true;
  }

  return false
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// const formatOrderStatus = order => {
//   if (order.orderStatus === 0) {
//     order.orderStatus = 0;
//     order.orderCnStatus = "已取消";
//   } else if (order.orderStatus === 1) {
//     order.orderStatus = 1;
//     order.orderCnStatus = "待付款";
//   } else if (order.orderStatus === 2) {
//     order.orderStatus = 98;
//     order.orderCnStatus = "待自提";
//   } else if (order.orderStatus === 5) {
//     order.orderStatus = 97;
//     order.orderCnStatus = "已自提";
//   } else if (order.orderStatus === 8) {
//     order.orderStatus = 4;
//     order.orderCnStatus = "已完成";
//   }
//   return order;
// }

const formatOrderStatus = order => {
  if (order.orderStatus === 1) {
    order.orderStatus = 0;
    order.orderCnStatus = "已取消";
  } else if (order.orderStatus === 0) {
    order.orderStatus = 1;
    order.orderCnStatus = "待付款";
  } else if (order.orderStatus === 5) {
    order.orderStatus = 98;
    order.orderCnStatus = "待提货";
  } else if (order.orderStatus === 9) {
    order.orderStatus = 97;
    order.orderCnStatus = "已提货";
  } else if (order.orderStatus === 8) {
    order.orderStatus = 96;
    order.orderCnStatus = "已关闭";
  }
  return order;
}

const stringToDate = (dateStr, separator) => {
  var timeStr = dateStr.split(' ')[1];
  dateStr = dateStr.split(' ')[0];
  if (!separator) {
    separator = "-";
  }
  var dateArr = dateStr.split(separator);
  var year = parseInt(dateArr[0]);
  var month = parseInt(dateArr[1]);
  var day = parseInt(dateArr[2]);

  var timeArr = timeStr.split(":");
  var hour = 0;
  var min = 0;
  var sec = 0;
  if (timeArr.length > 0) {
    hour = parseInt(timeArr[0])
  }
  if (timeArr.length > 1) {
    min = parseInt(timeArr[1])
  }
  if (timeArr.length > 2) {
    sec = parseInt(timeArr[2])
  }
  var date = new Date(year, month - 1, day, hour, min, sec);
  return date;
}

const wxAuth = (scope, callback) => {
  wx.getSetting({
    success: res => {
      if (!res.authSetting[scope]) {
        if (scope == "scope.userInfo") {
          callback()
          return;
        }
        wx.authorize({
          scope: scope,
          success() {
            callback()
          },
          fail() {
            let content = "";
            if (scope == "scope.writePhotosAlbum") {
              content = "需要您授权保存到相册的权限，请到小程序的设置中打开授权";
            } else if (scope == "scope.userLocation") {
              content = "需要您授权获取地理位置的权限，请到小程序的设置中打开授权";
            } else if (scope == "scope.userInfo") {
              content = "需要您授权获取用户信息的权限，请到小程序的设置中打开授权";
            }
            wx.showModal({
              title: '是否要打开设置页面',
              content: content,
              success: function(res) {
                if (res.confirm) {
                  wx.openSetting({
                    success(res) {
                      if (res.authSetting[scope]) {
                        callback()
                      } else {
                        wx.showToast({
                          title: "未获得用户授权,无法使用相应功能",
                          icon: 'none',
                          duration: 2000
                        });
                      }
                    }
                  });
                } else {
                  wx.showToast({
                    title: "未获得用户授权,无法使用相应功能",
                    icon: 'none',
                    duration: 2000
                  });
                }
              }
            })
          }
        })
      } else {
        callback()
      }
    }
  })
}

module.exports = {
  formatTime: formatTime,
  isBlank: isBlank,
  formatOrderStatus: formatOrderStatus,
  stringToDate: stringToDate,
  wxAuth: wxAuth
}